package com.boot.tool;

import com.boot.Mapper.YearMapper;
import com.boot.pojo.Month;
import com.boot.pojo.Year;
import org.apache.catalina.LifecycleState;
import org.springframework.beans.factory.annotation.Autowired;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 获取年柱 日柱 月柱 时柱
 *
 * @author yx
 */
public class BaziTool {
    private YearMapper yearMapper;
    private String[] dizhi = {"子", "丑", "寅", "卯", "辰", "巳", "午", "未", "申", "酉", "戌", "亥"};

    public BaziTool(YearMapper yearMapper) {
        this.yearMapper = yearMapper;
    }

    public BaziTool() {
    }

    /**
     * 获取年柱
     *
     * @param year 四位数公历年份
     * @return 该年的年柱
     */
    public String getYear(int year) {
        int index = (year - 3) % 60;
        List<Year> years = yearMapper.getYear(index);
        Year year1 = years.get(0);
        return year1.getGanzhi();
    }

    /**
     * 获取某个日期跟 1945-01-01 的差距
     *
     * @param date 要计算的日期
     * @return 差距的天数
     * @throws ParseException 异常
     */
    public long getDayDiff(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date end = sdf.parse(date);
        Date start = sdf.parse("1945-01-01 00:00:00");
        Calendar cal = Calendar.getInstance();
        cal.setTime(end);
        long nowTime = cal.getTimeInMillis();
        cal.setTime(start);
        long startTime = cal.getTimeInMillis();
        return (nowTime - startTime) / (1000 * 3600 * 24);
    }

    /**
     * 计算某个日期是什么时辰
     *
     * @param date 1995-12-04 00:00:00
     * @return 子
     */
    public String timeIndex(String date) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date d = sdf.parse(date);
        int shichen = d.getHours();
        return dizhi[shichen / 2];
    }
}
